<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/5/31
 * Time: 20:29
 */

namespace Admin\Controller;


use Think\AjaxPage;

class NoticeController extends BaseController
{
    /**
     * 公告列表
     */
    public function ajaxindex(){
        // 搜索条件
        $model = D('Notice');
        //  搜索条件下 分页赋值
//        foreach($condition as $key=>$val) {
//            $Page->parameter[$key]   =   urlencode($val);
//        }
        $sort_order = I('order_by','id').' '.I('sort','desc');
        $sort_order = ' tp_notice.'.$sort_order;
        $category_id = I('category_id');
        if(!empty($category_id)){
            $map['tp_notice.category_id'] = $category_id;
        }
        $count = $model->where($map)->count();
        $Page  = new AjaxPage($count,10);
        $noticeList = $model
            ->join('LEFT JOIN tp_notice_category c ON c.id=tp_notice.category_id')
            ->where($map)
            ->order($sort_order)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->field('tp_notice.*,c.name as category_name')
            ->select();
        foreach ($noticeList as $key => $value) {
            $noticeList[$key]['create_time'] = date("Y-m-d H:i:s", $noticeList[$key]['create_time']);
        }
        $show = $Page->show();
        $this->assign('noticeList',$noticeList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function index(){
        $categoryList = D('NoticeCategory')->select();
        $this->assign('categoryList',$categoryList);
        $this->display();
    }

    public function save_notice(){
        if(IS_POST){
            $model = D('Notice');
            $data = $model->create(I('post.'), 1);
            if($data['id']){
                $res = D('Notice')->save($data);
            }else {
                $data['create_time'] = time();
                $res = D('Notice')->add($data);
            }
            if($res){
                $this->success('保存成功',U('Notice/index'));exit;
            }else{
                $this->error('保存失败',U('Notice/index'));
            }
        }else{
            $id = I('id');
            if($id) {
                $notice = D('Notice')->find($id);
                $notice['content'] = html_entity_decode($notice['content']);
                $this->assign('notice', $notice);
            }
            $category_list = D('NoticeCategory')->select();
            $this->assign('category_list', $category_list);
        }
        $this->display();
    }

    public function edit_notice(){
        if(IS_POST){
            $data = I('post.');
            $res = D('Notice')->save($data);
            if($res){
                $this->success('保存成功',U('Notice/index'));exit;
            }else{
                $this->error('保存失败',U('Notice/index'));
            }
        }else{
            $id = I('id');
            $notice = D('Notice')->find($id);
            $this->assign('notice',$notice);
        }
        $this->display();
    }

    /**
     * 删除公告
     */
    public function delete(){
        $id = I('get.id');
        $row = M('Notice')->where(array('id'=>$id))->delete();
        if($row){
            $this->success('成功删除公告');
        }else{
            $this->error('操作失败');
        }
    }
}